package net.maku.kyy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.kyy.convert.KyyFixConvert;
import net.maku.kyy.entity.KyyFixEntity;
import net.maku.kyy.query.KyyFixQuery;
import net.maku.kyy.vo.KyyFixVO;
import net.maku.kyy.dao.KyyFixDao;
import net.maku.kyy.service.KyyFixService;
import com.fhs.trans.service.impl.TransService;
import net.maku.framework.common.utils.ExcelUtils;
import net.maku.kyy.vo.KyyFixExcelVO;
import net.maku.framework.common.excel.ExcelFinishCallBack;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 小程序首页游戏
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
@AllArgsConstructor
public class KyyFixServiceImpl extends BaseServiceImpl<KyyFixDao, KyyFixEntity> implements KyyFixService {
    private final TransService transService;

    @Override
    public PageResult<KyyFixVO> page(KyyFixQuery query) {
        IPage<KyyFixEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(KyyFixConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }


    private LambdaQueryWrapper<KyyFixEntity> getWrapper(KyyFixQuery query){
        LambdaQueryWrapper<KyyFixEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(query.getId()), KyyFixEntity::getId, query.getId());

        wrapper.eq(ObjectUtil.isNotEmpty(query.getName()), KyyFixEntity::getName, query.getName());
        wrapper.orderByDesc(KyyFixEntity::getId);

        return wrapper;
    }


    @Override
    public KyyFixVO get(Long id) {
        KyyFixEntity entity = baseMapper.selectById(id);
        KyyFixVO vo = KyyFixConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(KyyFixVO vo) {
        KyyFixEntity entity = KyyFixConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(KyyFixVO vo) {
        KyyFixEntity entity = KyyFixConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
    List<KyyFixExcelVO> excelList = KyyFixConvert.INSTANCE.convertExcelList(list());
        transService.transBatch(excelList);
        ExcelUtils.excelExport(KyyFixExcelVO.class, "小程序首页游戏", null, excelList);
    }

}